TWB.New_Module({
	///////////////////////////
	name : "MainMenu",       //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////

	settings : {
		autodetect_server : {
			type : "checkbox"
		}
	},
	Link : function(L){
		switch(L){
			case "server" : 
				L=TWB.HOST;
			break;
			
			case "wiki" :
				L=TWB.HOST+"?page_id=32";
			break;
			
			case "twstats" :
				if(TWB.language=="en"){
					L="http://twstats.com/";
				}
				else{
					if(TWB.language=="cs"){
						L="http://cz.twstats.com/";
					}
					else{
						L="http://"+TWB.language+".twstats.com/";
					}
				}
			break;
		}
		Load_Master_Url(L);
	},
	Load_Server : function(e){
		var server=TWB.Servers[e.getAttribute("name")];
		TWB.language=e.getAttribute("name");
		TWB.Modules.MainMenu.RefreshServerIcon();
		TWB.Load_Url("http://www."+server);
	},
	RefreshServerIcon : function(){
		var i;
		var NoDe=TWB.gei("TWB-Servers");
		for(i=0; i<NoDe.childNodes.length; i++){
			if(NoDe.childNodes[i].getAttribute("name")==TWB.language){
				var image=NoDe.childNodes[i].getAttribute("image");
				break;
			}
		}
		NoDe.parentNode.setAttribute("image",image);
	},
	Detect : function(){
		var server;
		for(server in TWB.Servers){
			if(TWB.logged(undefined,server)){
				TWB.language=server;
				TWB.Modules.MainMenu.RefreshServerIcon();
			}
		}
	},
	priority : 1,
	init : function(){
		// TWB Host
		TWB.Servers={};
		TWB.language="en";
		TWB.HOST="http://www.aisolutions.biz/";
		TWB.AJAX_HOST="http://www.aisolutions.biz/wordpress/";
		// Populate servers
		try{
			var NoDe=TWB.gei("TWB-Servers");
			var f1=TWB.Cc["@mozilla.org/file/local;1"].createInstance(TWB.Ci.nsILocalFile);
			var f2=TWB.Cc["@mozilla.org/file/local;1"].createInstance(TWB.Ci.nsILocalFile);
			var loc=TWB.Cc["@mozilla.org/file/directory_service;1"].getService(TWB.Ci.nsIProperties).get("ProfD", TWB.Ci.nsIFile).path+TWB.SL+"extensions"+TWB.SL+"twbooster@aisolutions.biz"+TWB.SL+"chrome"+TWB.SL+"locale";
			f1.initWithPath(loc);
			fs1=f1.directoryEntries;
			while(fs1.hasMoreElements()){
				var cut1=fs1.getNext().QueryInterface(Components.interfaces.nsILocalFile);
				if(cut1.isDirectory()){
					f2.initWithPath(cut1.path);
					fs2=f2.directoryEntries;
					var propz="";
					var lang=null;
					var server=null;
					var flag=null;
					while(fs2.hasMoreElements()){
						var cut2=fs2.getNext().QueryInterface(Components.interfaces.nsILocalFile).path;
						if(cut2.match(".lang")){
								lang=cut2.split(TWB.SL);
								lang=lang[lang.length-1].split(".lang")[0];
						}
						else{
							if(cut2.match("flag")){
								flag="file:"+SL+SL+cut2;
							}
							else{
								if(cut2.match("report.")){
									propz+=cut2.split("report.")[1];
								}
								else{
									if(!cut2.match(".dtd") && !cut2.match(".properties")){
										server=cut2.split(TWB.SL);
										server=server[server.length-1];
									}
								}
							}
						}
					}
					var EL=TWB.dce("menuitem");
					EL.setAttribute("id",propz);
					EL.setAttribute("name",lang);
					EL.setAttribute("class","menuitem-iconic");
					EL.setAttribute("image",flag);
					EL.setAttribute("label",server.charAt(0).toUpperCase()+server.slice(1));
					EL.setAttribute("oncommand","TWB.Modules.MainMenu.Load_Server(this)");
					NoDe.appendChild(EL);
					// Add to server list
					TWB.Servers[lang]=server;
				}
			}
			// Patch Server settings
			var tmp=[];
			for(lang in TWB.Servers){
				server=TWB.Servers[lang];
				tmp.push([lang,server.charAt(0).toUpperCase()+server.slice(1)]);
			}
			var i;
			for(i=0; i<TWB.settings.length; i++){
				if(TWB.settings[i][0]=="MainMenu"){
					TWB.settings[i][1].default_server={
						type : "menupop",
						values : tmp
					}
					break;
				}
			}
			// Load default server
			TWB.language=TWB.get_var("default_server","en");
			TWB.Modules.MainMenu.RefreshServerIcon();
			// Auto detect server
			if(TWB.get_var("autodetect_server")){
				TWB.Processor.start("ad_servers","Modules.MainMenu.Detect","*",false);
			}
		}catch(e){log("Main",e);}
	}
});